/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.rakev.matcher.factory;

import java.util.Arrays;
import java.util.Collection;
import ru.rakev.matcher.Matcher;

/**
 *
 * @author Rakev
 */
public class MatcherDecoratorFactory {

    private static MatcherDecoratorFactory instance = new MatcherDecoratorFactory();

    public static MatcherDecoratorFactory create() {
        return instance;
    }

    protected MatcherDecoratorFactory() {
    }

    public <T> Matcher<T> not(final Matcher<T> matcher) {
        return new Matcher<T>() {
            @Override
            public boolean matches(T object) {
                return !matcher.matches(object);
            }
        };
    }

    public <T> Matcher<T> allOf(final Matcher<T>... matchers) {
        return allOf(Arrays.asList(matchers));
    }

    public <T> Matcher<T> allOf(final Collection<? extends Matcher<T>> matchers) {
        return new Matcher<T>() {
            @Override
            public boolean matches(T object) {
                for (Matcher<T> matcher : matchers) {
                    if (!matcher.matches(object)) {
                        return false;
                    }
                }
                return true;
            }
        };
    }

    public <T> Matcher<T> anyOf(final Matcher<T>... matchers) {
        return anyOf(Arrays.asList(matchers));
    }

    public <T> Matcher<T> anyOf(final Collection<? extends Matcher<T>> matchers) {
        return new Matcher<T>() {
            @Override
            public boolean matches(T object) {
                for (Matcher<T> matcher : matchers) {
                    if (matcher.matches(object)) {
                        return true;
                    }
                }
                return false;
            }
        };
    }
}
